package servlet;

import dao.DefectiveProductDAOImpl;
import pojo.DefectiveProduct;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/defectiveProduct")
public class DefectiveProductServlet extends BaseServlet {

    // 添加次品
    protected void addDefectiveProduct(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int scrapNumber = Integer.parseInt(req.getParameter("scrapNumber"));
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));
        String reasons = req.getParameter("reasons");

        DefectiveProduct defectiveProduct = new DefectiveProduct(scrapNumber, itemNumber, reasons);
        DefectiveProductDAOImpl.add(defectiveProduct);
        sendJsonResponse(resp, new ApiResult(200, "Defective product added successfully.", null));
    }

    // 删除次品
    protected void deleteDefectiveProduct(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int scrapNumber = Integer.parseInt(req.getParameter("scrapNumber"));
        DefectiveProductDAOImpl.delete(scrapNumber);
        sendJsonResponse(resp, new ApiResult(200, "Defective product deleted successfully.", null));
    }

    // 更新次品
    protected void updateDefectiveProduct(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int scrapNumber = Integer.parseInt(req.getParameter("scrapNumber"));
        int itemNumber = Integer.parseInt(req.getParameter("itemNumber"));
        String reasons = req.getParameter("reasons");

        DefectiveProduct defectiveProduct = new DefectiveProduct(scrapNumber, itemNumber, reasons);
        DefectiveProductDAOImpl.update(defectiveProduct);
        sendJsonResponse(resp, new ApiResult(200, "Defective product updated successfully.", null));
    }

    // 获取所有次品
    protected void getAllDefectiveProducts(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<DefectiveProduct> defectiveProducts = DefectiveProductDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", defectiveProducts));
    }

    // 根据次品编号获取次品
    protected void getDefectiveProductById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int scrapNumber = Integer.parseInt(req.getParameter("scrapNumber"));
        DefectiveProduct defectiveProduct = DefectiveProductDAOImpl.getById(scrapNumber);
        sendJsonResponse(resp, new ApiResult(200, "Success", defectiveProduct));
    }

    // 获取次品数量
    protected void getDefectiveProductCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = DefectiveProductDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取次品
    protected void getDefectiveProductsByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<DefectiveProduct> defectiveProducts = DefectiveProductDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", defectiveProducts));
    }

    // 根据关键字搜索次品
    protected void findDefectiveProductsByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<DefectiveProduct> defectiveProducts = DefectiveProductDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", defectiveProducts));
    }

    // 根据关键字分页搜索次品
    protected void findDefectiveProductsByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<DefectiveProduct> defectiveProducts = DefectiveProductDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", defectiveProducts));
    }
}
